package com.oracle.config.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.oracle.config.service.api.SystemConfigApi;
import com.oracle.mapper.SysconfigMapper;
import com.oracle.pojo.Sysconfig;
import com.oracle.vo.PageVo;
import com.oracle.vo.ResultEntity;
import org.springframework.beans.factory.annotation.Autowired;

import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

@Service(filter = "systemServiceFilter")
public class SystemConfigServiceImpl implements SystemConfigApi {

    @Autowired
    private SysconfigMapper sysconfigMapper;

    @Override
    public ResultEntity<PageVo<Sysconfig>> queryPage(Integer pageIndex, Integer pageSize) {
        Page page = PageHelper.startPage(pageIndex, pageSize);
        this.sysconfigMapper.selectByExample(null);
        PageVo<Sysconfig> pageVo = new PageVo<Sysconfig>();
        Integer total = Integer.parseInt(page.getTotal()+"");
        pageVo.setpageSize(page.getPageSize());
        pageVo.setPageIndex(page.getPageNum());
        pageVo.setObjList(page.getResult());
        pageVo.setDataTotal(total);
        pageVo.setPageTotal(total%pageSize==0?total/pageSize:total/pageSize+1);
        ResultEntity<PageVo<Sysconfig>> resultEntity = new ResultEntity<>(ResultEntity.CODE_SUCCESS,ResultEntity.MSG_SUCCESS,pageVo);
        return resultEntity;
    }

    @Override
    public ResultEntity add(Sysconfig sysconfig) {
        sysconfig.setCreatedate(new Timestamp(new Date().getTime()));
        sysconfig.setModifydate(new Timestamp(new Date().getTime()));
        this.sysconfigMapper.insertSelective(sysconfig);
        return new ResultEntity(ResultEntity.CODE_SUCCESS,ResultEntity.MSG_SUCCESS);
    }

    @Override
    public ResultEntity update(Sysconfig sysconfig) {
        this.sysconfigMapper.updateByPrimaryKeySelective(sysconfig);
        return new ResultEntity(ResultEntity.CODE_SUCCESS,ResultEntity.MSG_SUCCESS);
    }

    @Override
    public ResultEntity<Sysconfig> getSysconfigById(Integer id) {
        Sysconfig sysconfig = this.sysconfigMapper.selectByPrimaryKey(id);
        return new ResultEntity<Sysconfig>(ResultEntity.CODE_SUCCESS,ResultEntity.MSG_SUCCESS,sysconfig);
    }

    @Override
    public ResultEntity<List<Sysconfig>> getSysconfigAll() {
        List<Sysconfig> sysconfigList = this.sysconfigMapper.selectByExample(null);
        return new ResultEntity<List<Sysconfig>>(ResultEntity.CODE_SUCCESS,ResultEntity.MSG_SUCCESS,sysconfigList);
    }

    @Override
    public ResultEntity<Integer> getFileCountMaxValue() {
        Integer fileCountMaxValue = this.sysconfigMapper.selectFileCountMaxValue();
        return new ResultEntity<Integer>(ResultEntity.CODE_SUCCESS,ResultEntity.MSG_SUCCESS,fileCountMaxValue);
    }

    @Override
    public ResultEntity<Integer> getLoopMaxValue() {
        Integer loopMaxValue = this.sysconfigMapper.selectLoopMaxValue();
        return new ResultEntity<Integer>(ResultEntity.CODE_SUCCESS,ResultEntity.MSG_SUCCESS,loopMaxValue);
    }


}
